<?php

namespace app\api\service;

use think\facade\Db;

class IntegralGoodsService
{

    /**
     * 获取积分商城商品
     * */
    public static function getIntegralGoodsList($param)
    {
         $query = Db::name('goods')
                            ->field('b.id,a.goods_name,a.main_image,a.sell_price,b.redeem_integral,b.redeem_price,b.redeem_num')
                            ->alias('a')
                            ->join('integral_goods b','a.id=b.goods_id')
                            ->withAttr('main_image',function ($value){
                                return request()->domain() . $value;
                            })->where('b.store_id',$param['store_id']);


         if (!empty($param['start_interval']) && !empty($param['end_interval'])){
            $query->whereBetween('b.redeem_integral',[$param['start_interval'],$param['end_interval']]);
         }

        $integralGoodsList = $query->order(['b.sort' => 'asc','b.redeem_num' => 'desc','b.id' => 'desc'])->paginate(10);

        return $integralGoodsList;
    }

    /**
     * 获取秒杀商品详情
     *
     * */

    public static function findIntegralGoods($id)
    {
        $integralGoods = Db::name('goods')
                        ->field('b.id,a.goods_name,a.main_image,a.spec_type,a.goods_images,a.goods_attr,a.sell_price,
                        b.redeem_integral,b.redeem_price,b.redeem_stock,b.redeem_num,b.status,c.content')
                        ->alias('a')
                        ->join('integral_goods b','a.id=b.goods_id')
                        ->join('goods_content c','a.id=c.goods_id')
                        ->where('b.id',$id)
                        ->withAttr('main_image',function ($value){
                            return request()->domain() . $value;
                        })->withAttr('goods_attr',function ($value){
                            if(empty($value)){
                                return [];
                            }
                            return json_decode($value,true);
                        })->withAttr('goods_images',function ($value){
                            $imgs = [];
                            $arr = json_decode($value,true);
                            foreach ($arr as $v){
                                $imgs[] = request()->domain() . $v;
                            }
                            return $imgs;
                        })->find();

        $items = [];
        if ($integralGoods['spec_type']==2){
            $items = Db::name('goods_item')
                        ->alias('a')
                        ->field('b.id,a.attr,a.image,a.sell_price,b.redeem_integral,b.redeem_price,b.redeem_stock')
                        ->join('Integral_goods_item b','a.id=b.sku_id')
                        ->withAttr('image',function ($value){
                            return request()->domain() . $value;
                        })->where('b.Integral_goods_id',$integralGoods['id'])
                        ->select();
        }

        $integralGoods['items'] = $items;


        return $integralGoods;

    }
}